package com.example.demomk.Utils;


import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.demomk.R;

import java.util.List;

public class CustomSpinner {
    private Context context;
    private View spinnerView;
    private TextView tvSelectedItem;
    private PopupWindow popupWindow;
    private ListView listView;
    private List<String> items;
    private OnItemSelectedListener listener;

    public interface OnItemSelectedListener {
        void onItemSelected(int position, String item);
    }

    public CustomSpinner(Context context, ViewGroup parent, List<String> items) {
        this.context = context;
        this.items = items;
        initView(parent);
        initPopupWindow();
    }

    private void initView(ViewGroup parent) {
        spinnerView = LayoutInflater.from(context).inflate(R.layout.custom_spinner, parent, false);
        tvSelectedItem = spinnerView.findViewById(R.id.tvSelectedItem);
        spinnerView.setOnClickListener(v -> showDropdown());
    }

    private void initPopupWindow() {
        View popupView = LayoutInflater.from(context).inflate(R.layout.spinner_dropdown, null);
        listView = popupView.findViewById(R.id.listView);

        ArrayAdapter<String> adapter = new ArrayAdapter<>(context, R.layout.spinner_dropdown_item, items);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener((parent, view, position, id) -> {
            tvSelectedItem.setText(items.get(position));
            if (listener != null) {
                listener.onItemSelected(position, items.get(position));
            }
            popupWindow.dismiss();
        });

        popupWindow = new PopupWindow(
                popupView,
                spinnerView.getWidth(),
                (int) (200 * context.getResources().getDisplayMetrics().density)
        );

        // 关键修改点1：设置正确的Window Token
        popupWindow.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL);

        // 关键修改点2：设置背景和可触摸
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);

        // 关键修改点3：在Dialog中需要这样显示
        spinnerView.setOnClickListener(v -> {
            if (popupWindow != null) {
                // 确保使用正确的锚点视图
                popupWindow.showAsDropDown(spinnerView, 0, 0, Gravity.BOTTOM);
            }
        });
    }

    private void showDropdown() {
        if (popupWindow != null) {
            popupWindow.showAsDropDown(spinnerView, 0, 0);
        }
    }

    public View getView() {
        return spinnerView;
    }

    public void setOnItemSelectedListener(OnItemSelectedListener listener) {
        this.listener = listener;
    }

    public void setSelection(int position) {
        if (position >= 0 && position < items.size()) {
            tvSelectedItem.setText(items.get(position));
        }
    }

    public void updateData(List<String> newItems) {
        this.items = newItems;
        ArrayAdapter<String> adapter = new ArrayAdapter<>(context, R.layout.spinner_dropdown_item, newItems);
        listView.setAdapter(adapter);
    }
}